package exporter

import (
	"emc_vnx_exporter/utils"
	"github.com/prometheus/client_golang/prometheus"
	"io/ioutil"
	"log"
	"os/exec"
	"strings"
	"time"
)

type LCC struct {
	Name     string
	State    string
	Revision string
	Serial   string
}

var (
	lcc = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "vnx_get_all_lcc",
			Help: "Get All LCC Info",
		},
		[]string{"Name", "State", "Revision", "Serial"},
	)
)

func LCCGauge() *prometheus.GaugeVec {
	return lcc
}

func GetAllLCCMetrics(spIp string, user string, password string) {
	for {
		cmd := exec.Command("bash", "-c", "/opt/Navisphere/bin/naviseccli -h "+spIp+" -user "+user+" -password "+password+" -Scope 0 getcrus | grep 'LCC*'")
		stdout, _ := cmd.StdoutPipe()
		defer stdout.Close()
		if err := cmd.Start(); err != nil {
			log.Fatalf("cmd.Start: %v", err)
		}
		result, _ := ioutil.ReadAll(stdout) // 读取输出结果
		resData := string(result)

		var lccNameList []string
		lccInfoList := strings.Split(resData, "\n")
		for _, lccInfo := range lccInfoList {
			if strings.Contains(lccInfo, "State:") {
				lccNameList = append(lccNameList, strings.Split(lccInfo, "State: ")[0])
			}
		}

		for _, lccName := range lccNameList {
			data := new(LCC)
			data.Name = lccName
			for _, lccString := range lccInfoList {
				if strings.Contains(lccString, lccName) {
					if strings.Contains(lccString, "State: ") {
						data.State = strings.Split(lccString, "State: ")[1]
					} else if strings.Contains(lccString, "Revision: ") {
						data.Revision = strings.Split(lccString, "Revision: ")[1]
					} else if strings.Contains(lccString, "Serial #: ") {
						data.Serial = strings.Split(lccString, "Serial #: ")[1]
					}
				}
			}
			lcc.WithLabelValues(data.Name, data.State, data.Revision, data.Serial).Set(utils.CrusStateToFloat(data.State))
		}

		time.Sleep(60 * time.Second)
	}
}
